package com.springboot.admin.controller;

import com.springboot.admin.common.ResponseMap;
import com.springboot.admin.entity.WebStatsDto;
import com.springboot.admin.service.WebStatsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/stats/")
@Api(tags = "统计接口")
public class WebStatsController {

    @Autowired
    WebStatsService webStatsService;

    @GetMapping("/getStats.gif")
    @ApiOperation(value="埋点接口")
    public ResponseMap getStats(HttpServletRequest request, @RequestParam Map map) {

        String ip = getIpAddr(request);
        log.info("ip:" + ip);

        WebStatsDto webStatsDto = new WebStatsDto();

        webStatsDto.setTitle((String) map.get("title"));
        webStatsDto.setDomain((String) map.get("domain"));
        webStatsDto.setReferrer((String) map.get("referrer"));
        webStatsDto.setScreen((String) map.get("screen"));
        webStatsDto.setLang((String) map.get("lang"));
        webStatsDto.setUserAgent((String) map.get("userAgent"));
        webStatsDto.setOs((String) map.get("os"));
        webStatsDto.setBrowse((String) map.get("browse"));
        webStatsDto.setDevice((String) map.get("device"));
        webStatsDto.setHref((String) map.get("hrefs"));
        webStatsDto.setIp(ip);
        webStatsDto.setVisitorId((String) map.get("visitorId"));

//        log.info(webStatsDto.toString());
        webStatsService.insertWebStats(webStatsDto);
        return ResponseMap.success();
    }

    @GetMapping("/getWebStatistics")
    @ApiOperation(value="查询统计接口")
    public ResponseMap getWebStatistics() {
        return ResponseMap.success( webStatsService.getWebStatistics());
    }

    public String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("X-Real-IP");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("X-Forwarded-For");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("REMOTE-HOST");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress="";
        }
        return ipAddress;
    }
}
